(function() {
  var canScroll, clicked,
    __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

  canScroll = true;

  this.scroll = function(y, reScrllTime) {
    var iY, oldY, timer;
    if (reScrllTime == null) {
      reScrllTime = 6000;
    }
    if (true === y || false === y) {
      canScroll = y;
      if (!y && reScrllTime) {
        if (reScrllTime < 100) {
          reScrllTime *= 1000;
        }
        setTimeout(function() {
          return canScroll = true;
        }, reScrllTime);
      }
      return false;
    }
    oldY = document.body.scrollTop;
    iY = 0;
    return timer = setInterval(function() {
      var newY;
      if (!canScroll) {
        return false;
      }
      newY = document.body.scrollTop;
      document.body.scrollTop += 1 + Math.ceil(Math.random() * 5);
      if (newY === document.body.scrollTop) {
        iY++;
      } else {
        iY = 0;
      }
      if (document.body.scrollTop - oldY > y || iY > 1000) {
        return clearInterval(timer);
      }
    }, 20);
  };

  this.scrollTop = function(y, relative) {
    if (relative == null) {
      relative = false;
    }
    if (relative) {
      return document.body.scrollTop += y;
    } else {
      return document.body.scrollTop = y;
    }
  };

  this.capture = function(callback, timeDelay, beforCallback) {
    if (timeDelay == null) {
      timeDelay = 0;
    }
    if (beforCallback == null) {
      beforCallback = '';
    }
    if (callback > 0) {
      if ($.isFunction(timeDelay)) {
        beforCallback = timeDelay;
      }
      timeDelay = callback;
    }
    return setTimeout(function() {
      if (beforCallback) {
        beforCallback();
      }
      chrome.extension.sendRequest({
        capture: 1
      });
      console.debug(new Date().toLocaleTimeString());
      console.count('capture');
      if ($.isFunction(callback)) {
        return setTimeout(function() {
          return callback();
        }, 500);
      }
    }, timeDelay * 1000);
  };

  this.getItemId = function(url) {
    var r, regex;
    regex = /[?&]id=(\d+)/ig;
    r = regex.exec(url);
    if (r) {
      return r[1];
    } else {
      return 0;
    }
  };

  clicked = false;

  this.clickOther = function() {
    var meId;
    console.debug('clickOther');
    meId = getItemId(location.href);
    return chrome.extension.sendRequest({
      clickId: meId,
      clickedIds: 1,
      leftView: 1
    }, function(response) {
      var a, allAs, clickA, selectorStr, _i, _j, _len, _len1, _ref;
      console.debug('receive click msg ', new Date().toLocaleTimeString(), response);
      if (response && response.buy && response.buy.leftView > 0) {
        clickA = function(a) {
          var id, img;
          id = getItemId(a.href);
          img = $('img', a)[0];
          if (id > 0 && __indexOf.call(response.buy.clickedIds, id) < 0 && id !== meId && img && a.href.indexOf('item') > -1) {
            simulateClick(img, {
              ctrl: true
            });
            clicked = true;
            chrome.extension.sendRequest({
              clickId: getItemId(a.href)
            });
            return false;
          }
          return true;
        };
        _ref = ['a[href^="http://ju.mmstat.com"]', 'a[href^="http://item.taobao.com"]'];
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          selectorStr = _ref[_i];
          if (clicked) {
            break;
          }
          allAs = $(selectorStr).toArray();
          allAs.sort(function() {
            return Math.random() - 0.5;
          });
          for (_j = 0, _len1 = allAs.length; _j < _len1; _j++) {
            a = allAs[_j];
            if (!clickA(a)) {
              break;
            }
          }
        }
        if (clicked) {
          return console.debug('clicked');
        } else {
          return console.error('noClicked');
        }
      } else {
        return console.debug('no click response');
      }
    });
  };

}).call(this);
